<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            position: relative;
            background-color: #f5f5f5;
        }

        h1 {
            text-align: center;
            margin-top: 30px;
            color: #e38a0e;
        }

        .box {
            width: 320px;
            height: 400px;
            margin: 100px auto;
            background-color: white;
        }

        .first {
            margin-top: 30px;
            width: 270px;
            height: 40px;
            display: flex;
            border: 1px solid #e38a0e;
            margin-left: 30px;
        }

        .first input {
            width: 208px;
            height: 38px;
            border: none;
            outline: none;
            padding-left: 10px;
        }

        .first button {
            background-color: #e38a0e;
            font-size: 13px;
            color: #fff;
            border: none;
        }

        .content {
            width: 270px;
            height: 100px;
            margin-left: 30px;
        }

        .content ul li {

            position: relative;
            border-bottom: 1px solid #2b2b2a;
            list-style: none;
            line-height: 30px;
            width: 270px;
            height: 30px;
            display: flex;
        }

        .content ul li button {
            position: absolute;
            right: 0;
            top: 5px;
            width: 70px;
            height: 20px;
            background-color: white;
            color: #e38a0e;
            border: none;

        }

        .last {
            width: 270px;
            height: 30px;
            display: flex;
            justify-content: space-between;
            margin-top: 10px;
        }

        .last button {
            width: 70px;
            height: 20px;
            background-color: white;
            color: #e38a0e;
            border: none;
        }

        .last span {
            color: #e38a0e;
        }

        a {
            position: fixed;
            right: 50%;
            bottom: 5%;
            display: block;
            width: 100px;
            height: 30px;
            background-color: #e38a0e;
            color: #fff;
            text-align: center;
            line-height: 30px;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <div id="app">
        <div class="box">
            <h1>待办事项</h1>
            <div class="first">
                <input type="text" placeholder="请输入任务">
                <button @click="add">添加任务</button>
            </div>
            <div class="content">
                <ul>
                    <li v-for="(item,index) in list" :key="id">
                        {{index+1}}. {{item.title}}
                        <button @click="del(item.id)">x</button>
                    </li>
                </ul>
                <div class="last">
                    <span>合计:{{count}}</span>
                    <button v-model="list" @click="clear">清空任务</button>
                </div>
            </div>
        </div>
        <a href="/首页.html">返回</a>
        <script>
            const { createApp } = Vue
            createApp({
                data() {
                    return {
                        index: 0,
                        list: [

                        ],
                        count: 0
                    }
                },
                created() {
                    let data = localStorage.getItem('mission')
                    if (data) {
                        this.list = JSON.parse(data)
                        this.count = this.list.length
                    }
                },
                methods: {
                    add() {
                        let val = document.querySelector('input').value
                        if (val != '') {
                            this.index++
                            this.list.push({
                                id: this.index,
                                title: val
                            })
                            this.count++
                            localStorage.setItem('mission', JSON.stringify(this.list))
                        }
                        document.querySelector('input').value = ''

                    },
                    del(id) {
                        // this.list = this.list.splice(id, 1)
                        this.list = this.list.filter(item => item.id !== id)
                        localStorage.setItem('mission', JSON.stringify(this.list))
                        this.count--
                    },
                    clear() {
                        this.list = []
                        localStorage.setItem('mission', JSON.stringify(this.list))
                        this.count = 0
                    }
                }
            }).mount('#app')
        </script>
</body>

</html>